package edu.ncsu.csc.itrust.dao.labprocedure;

import junit.framework.TestCase;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.TreeSet;
import edu.ncsu.csc.itrust.enums.LabProcedureStatus;
import edu.ncsu.csc.itrust.enums.Role;
import edu.ncsu.csc.itrust.beans.LabProcedureBean;
import edu.ncsu.csc.itrust.beans.LabTechProcedureBean;
import edu.ncsu.csc.itrust.beans.PersonnelBean;
import edu.ncsu.csc.itrust.dao.DAOFactory;
import edu.ncsu.csc.itrust.dao.mysql.LabProcedureDAO;
import edu.ncsu.csc.itrust.dao.mysql.PersonnelDAO;
import edu.ncsu.csc.itrust.datagenerators.TestDataGenerator;
import edu.ncsu.csc.itrust.testutils.TestDAOFactory;

public class UC33DAOTest extends TestCase {
	
	private DAOFactory factory = null;
	private LabProcedureDAO dao = null;
	private TestDataGenerator gen = null;
	private PersonnelDAO pdao = null;

	
	public void setUp(){
		try{
			factory = TestDAOFactory.getTestInstance();
			dao = factory.getLabProcedureDAO();
			pdao = factory.getPersonnelDAO();
			gen = new TestDataGenerator();
			gen.clearAllTables();
			gen.standardData();			
		}catch (Exception e){
			fail("Exception setup");
		}
		
	}
	/**
	 * for testing update priority method
	 */
	public void testUpdatePriority(){
		
		try{
			LabProcedureBean b = dao.getLabProceduresForPatient(1).get(0);
			dao.updatePriority(b.getProcedureID(),1);
			LabProcedureBean b1 = dao.getLabProcedure(b.getProcedureID());
			assertEquals(1L, b1.getPriority());
		}catch (Exception e){
			fail("SQLExcception update Pri");
		}
	}
	
	/**
	 * for testing getPriority method
	 */
	public void testGetPriority(){
		
		try{
			LabProcedureBean b = dao.getLabProceduresForPatient(1).get(0);
			dao.updatePriority(b.getProcedureID(),2);
			assertEquals(2,dao.getPriority(b.getProcedureID()));			
		}catch (Exception e){
			fail("SQLException priority");
		}
	}
	
	/**
	 * for testing updateStatus method
	 * 
	 */
	public void testUpdateStatus(){
		try{
			LabProcedureBean b = dao.getLabProceduresForPatient(1).get(0);
			dao.updateStatus(b.getProcedureID(),LabProcedureStatus.Pending.toString());
			assertEquals(dao.getLabProcedure(b.getProcedureID()).getStatus(),"PENDING");
			LabProcedureStatus.Pending.parse("PENDING");
			LabProcedureStatus.Pending.parse("COMPLETED");
		}catch (Exception e){
			fail("SQLException status");
		}
	}
	/**
	 * for testing complete procedure method
	 * 
	 */
	public void testCompleteProcedure(){
		try{
			LabProcedureBean b = dao.getLabProceduresForPatient(1).get(0);
			dao.completeProcedure(b.getProcedureID(),"success");
			LabProcedureBean b1 = dao.getLabProcedure(b.getProcedureID());
			assertEquals(b1.getStatus(),"COMPLETED");
			assertEquals(b1.getCommentary(),"success");
			
		}catch (Exception e){
			fail("SQLException complete");
		}
	}
	/**
	 * for testing updateAssignedTech
	 */
	public void testUpdateAssignedTech(){
		try{
			LabProcedureBean b = dao.getLabProceduresForPatient(1).get(0);
			dao.updateAssignedTech(b.getProcedureID(),5L);
			assertEquals(5L,dao.getLabProcedure(b.getProcedureID()).getLtID());			
		}catch (Exception e){
			fail("SQLException update");
		}
	}
	/**
	 * for testing report procedure results
	 */
	public void testReportProcedureResults(){
		try{
			LabProcedureBean b = dao.getLabProceduresForPatient(1).get(0);
			dao.reportProcedureResults(b.getProcedureID(),"success");
			assertEquals("PENDING", dao.getLabProcedure(b.getProcedureID()).getStatus());
			assertEquals("success",dao.getLabProcedure(b.getProcedureID()).getResults());
		}catch (Exception e){
			fail("SQLException report");
		}
	}
	/**
	 * for testing getPatientPending
	 * 
	 */
	public void testPatientPending(){
		try{
			List<LabProcedureBean> list = dao.getPatientPending(1L);
			assertEquals(1,list.size());
		}catch (Exception e){
			fail("SQLException patient pending");
		}
	}
	/**
	 * for testing getAllLabTechWithProcedures
	 */
	public void testGetTechProcedure(){
		try{
			LabProcedureBean b = dao.getLabProceduresForPatient(1).get(0);
			Role role = Role.LT;
			long mid = pdao.addEmptyPersonnel(role);
			PersonnelBean bean = pdao.getPersonnel(mid);
			dao.updateAssignedTech(b.getProcedureID(),mid);
			dao.updatePriority(b.getProcedureID(),1);
			List<LabTechProcedureBean> list = new ArrayList<LabTechProcedureBean>();
			TreeSet<LabTechProcedureBean> techSet = dao.getAllLabTechWithProcedures();
			while(techSet!= null && !techSet.isEmpty() )
				list.add(techSet.pollFirst());			
//			assertTrue(list.size() == 1);
//			assertEquals(1,list.get(0).getTotalProcedures());
//			assertEquals(1,list.get(0).getTotalP1());			
		}catch (Exception e){
			e.printStackTrace();
			fail("SQLException gell all with procedures");
		}
	}
	
	public void testLoading() {
		Connection conn = null;
		PreparedStatement ps = null;
		try{
			conn = factory.getConnection();
			ps = conn.prepareStatement("SELECT * FROM LabProcedure WHERE PatientMID = ? AND Rights = ? ORDER BY UpdatedDate DESC");
			ps.setLong(1, 2L);
			ps.setString(2, LabProcedureBean.Allow);
			ResultSet rs = ps.executeQuery();
			//LabTechProcedureBeanLoader loader = new LabTechProcedureBeanLoader();
			//List<LabTechProcedureBean> LTPROC = loader.loadList(rs);
		} catch (Exception e) {
		e.printStackTrace();
		fail("SQLException loading list and/or single");
		}
	}
}

